# -*- coding: utf-8 -*-
# @Author: fanmingxiang@touty.io
# @Date: 2023/4/6 14:21
import base64
import os
import smtplib
from django.core.mail import EmailMultiAlternatives
from django.template.loader import get_template
from ApiAutoTestPlatform import settings
from test_reports.models import TestReports, ReportDescribe
from django.core.mail import EmailMessage


def send_report(report_id, user_email):
    # 第三方 SMTP 服务
    mail_host = "smtp.163.com"  # SMTP 服务器地址
    mail_user = "17630913145@163.com"  # 邮箱账号
    mail_pass = "OCUBEFONNEIHQTLV"  # 客户端授权码，非登录密码

    test_reports = TestReports.objects.get(id=report_id)
    report_describe = ReportDescribe.objects.filter(test_report=test_reports)
    content = {
        "title": test_reports.title,
        "startTime": f"{test_reports.startTime}",
        "duration": test_reports.stopTime - test_reports.startTime,
        "test_all_count": test_reports.test_all_count,
        "success_count": test_reports.success_count,
        "failure_count": test_reports.failure_count,
        "error_count": test_reports.error_count,
        "pass_rate": f"{test_reports.success_count / test_reports.test_all_count:.2%}",
        "pass_rate_value": f"{test_reports.success_count / test_reports.test_all_count:.2%}"[:-1],
        "report_describe": report_describe

    }
    html_path = settings.BASE_DIR + r"\apitests\templates\report.html"
    # 渲染 HTML 模板
    template = get_template(html_path)
    html = template.render(content)
    # 邮件内容
    sender = mail_user  # 发件人邮箱
    receivers = ["fanmingxiang@touty.io", user_email, ]  # 收件人邮箱列表
    subject = test_reports.title + "测试报告"
    text_content = "你好，这是您的测试报告，附件请下载至本地查看"  # 纯文本消息
    msg = EmailMessage(subject=subject, body=text_content, from_email=sender, to=receivers,
                       attachments=[(html_path, html, 'text/html')])

    # 发送邮件
    try:
        smtpObj = smtplib.SMTP()  # 创建 SMTP 对象
        smtpObj.connect(mail_host, 25)  # 连接到 SMTP 服务器
        smtpObj.login(mail_user, mail_pass)  # 登录发送邮件账号
        smtpObj.sendmail(sender, receivers, msg.message().as_string())  # 发送邮件
        print("邮件发送成功！")
    except smtplib.SMTPException as e:
        print("Error: 无法发送邮件.", e)


def send_report1(report_id, user_email):
    # 第三方 SMTP 服务
    mail_host = "smtp.163.com"  # SMTP 服务器地址
    mail_user = "17630913145@163.com"  # 邮箱账号
    mail_pass = "OCUBEFONNEIHQTLV"  # 客户端授权码，非登录密码

    test_reports = TestReports.objects.get(id=report_id)
    report_describe = ReportDescribe.objects.filter(test_report=test_reports)
    content = {
        "title": test_reports.title,
        "startTime": f"{test_reports.startTime}",
        "duration": test_reports.stopTime - test_reports.startTime,
        "test_all_count": test_reports.test_all_count,
        "success_count": test_reports.success_count,
        "failure_count": test_reports.failure_count,
        "error_count": test_reports.error_count,
        "pass_rate": f"{test_reports.success_count / test_reports.test_all_count:.2%}",
        "pass_rate_value": f"{test_reports.success_count / test_reports.test_all_count:.2%}"[:-1],
        "report_describe": report_describe

    }
    # 渲染 HTML 模板
    template = get_template(settings.BASE_DIR + r"\apitests\templates\report.html")
    html = template.render(content)
    # 邮件内容
    sender = mail_user  # 发件人邮箱
    receivers = ["fanmingxiang@touty.io", user_email, ]  # 收件人邮箱列表
    subject = "Python SMTP 邮件测试"
    text_content = "你好，这是一封测试邮件！"  # 纯文本消息
    msg = EmailMultiAlternatives(subject=subject, body=text_content, from_email=sender, to=receivers)
    msg.attach_alternative(html, "text/html")  # HTML 内容

    # 发送邮件
    try:
        smtpObj = smtplib.SMTP()  # 创建 SMTP 对象
        smtpObj.connect(mail_host, 25)  # 连接到 SMTP 服务器
        smtpObj.login(mail_user, mail_pass)  # 登录发送邮件账号
        msg.encoding = 'utf-8'
        smtpObj.sendmail(sender, receivers, msg.message().as_string())  # 发送邮件
        print("邮件发送成功！")
    except smtplib.SMTPException as e:
        print("Error: 无法发送邮件.", e)
